package com.grouuu.loader 
{
	import com.grouuu.vo.LoadMediaVO;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	/**
	 * ...
	 * @author Grouuu
	 */
	public class LoadMedia extends EventDispatcher 
	{
		// TODO :
		// • doit loader toutes sortes de formats (jpg, xml, fnt, etc.)
		//   → un Loader et un URLLoader
		// • doit mettre ce media dans un ResourceLocator, avec un ID
		
		public function LoadMedia() 
		{
			//
		}
		
		// LOAD ///////////////////////////////////////////////////////////////////////////////////
		
		public function loadFile(vo:LoadMediaVO):void
		{
			var loader:Loader = new Loader();
			
			addListeners(loader.contentLoaderInfo);
			
			var request:URLRequest = new URLRequest(vo.url);
			
			loader.load(request);
		}
		
		public function loadDocument(vo:LoadMediaVO):void
		{
			var loader:URLLoader = new URLLoader();
			
			addListeners(loader);
			
			var request:URLRequest = new URLRequest(vo.url);
			
			loader.load(request);
		}
		
		// LISTENERS //////////////////////////////////////////////////////////////////////////////
		
		protected function addListeners(loader:EventDispatcher):void
		{
			loader.addEventListener(Event.COMPLETE, onComplete);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
			loader.addEventListener(ProgressEvent.PROGRESS, onProgress);
			
			if (loader is URLLoader)
				loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
		}
		
		// EVENTS /////////////////////////////////////////////////////////////////////////////////
		
		private function onComplete(event:Event):void 
		{
			trace("complete");
		}
		
		private function onIOError(event:IOErrorEvent):void 
		{
			trace("ioerror : " + event.text);
		}
		
		private function onProgress(event:ProgressEvent):void 
		{
			trace("progress");
		}
		
		private function onSecurityError(event:SecurityErrorEvent):void 
		{
			trace("security error");
		}
	}
}